import time

# 商品信息
shop_lst1 = {'name': '苹果', 'price': 1.2}
shop_lst2 = {'name': '香蕉', 'price': 5.5}
shop_lst3 = {'name': '梨子', 'price': 3.2}
shop_info = {'F00001': shop_lst1, 'F00002': shop_lst2, 'F00004': shop_lst3}


# 已购清单
buy_lst = {'F00004': shop_lst3}

# 用户信息
user_lst1 = ['123', 1.0, buy_lst]
user_lst2 = ['456', 100.0, buy_lst]
user_info = {'a': user_lst1, 'b': user_lst2}


# 用户登录
def user_login():
    print("用户登录".center(20, '-'))
    select = input("回车键继续,输入exit退出".center(20, '-'))
    if select == 'exit':
        exit()
    name = input("请输入用户名")
    pwd = input("请输入密码")
    if name in user_info and user_info[name][0] == pwd:
        print("登录成功!")
        return name
    else:
        print("用户名或者密码输入有误!请重新输入!")
        return 0


def menu():
    print("欢迎进入三乐购物系统".center(20, '-'))
    print("1.查看商品列表".center(20))
    print("2.查看已购清单".center(20))
    print("3.exit退出".center(20))
    select = input("对应键继续,‘exit’退出")
    if select == '1':
        return 1
    elif select == '2':
        return 2
    else:
        tplt = "{0:^5}\t{1:^5}"
        print(tplt.format("账号","余额"))
        print(tplt.format(name,user_info[name][1]))
        exit()


def shopping(name):
    # 设定商品总和为0
    shop_sum = 0.0
    money = 0.0
    print("商品清单".center(20, '-'))
    tplt = "{0:^5}\t{1:^5}\t{2:^5}"
    print(tplt.format("编号", "名字", "价格"))
    for i in shop_info:
        print(tplt.format(i, shop_info[i]['name'], shop_info[i]['price']))
    no = input("请输入需要的商品编号以','分割")
    shopping_list = no.split(',')
    # 判断商品编号是否存在
    for i in shopping_list:
        flag = shop_info.get(i, 0)
        if flag == 0:
            print(f"商品编号{i}输入有误")
            # 将信息有误商品删除
            shopping_list.remove(i)
            if len(shopping_list) == 0:
                print("商品错误!即将返回菜单".center(20,'-'))
                return 0
        else:
            # 获取用户金额
            money = user_info[name][1]
            shop_sum = shop_info[i]['price'] + shop_sum
    if shop_sum <= money:
        print("支付成功!")
        print(tplt.format("总和", "余额", "单位"))
        user_info[name][1] = money - shop_sum
        print(tplt.format(shop_sum, user_info[name][1], '￥'))
        # 存入已购清单
        for i in shopping_list:
            buy_thing = {}
            buy_thing['name'] = shop_info[i]['name']
            buy_thing['price'] = shop_info[i]['price']
            buy_lst[i] = buy_thing
        return 1
    else:
        print("支付失败!余额不足!")
        print(f"当前可用余额为:{user_info[name][1]}")
        b = input(("是否进行充值服务:(是/否)(1/0)"))
        if b == '0':
            print("即将返回主菜单".center(20,'-'))
            return 0
        elif b == '1':
            print("将进入充值系统".center(20,'-'))
            deposit_money(name)
        else:
            print("输入错误!即将返回主菜单".center(20, '-'))
            return 0


# 打印已买清单
def buy_list(name):
    tplt = "{0:^5}\t{1:^5}\t{2:^5}"
    print("已购清单".center(20, '-'))
    print(tplt.format("编号", "名字", "价格"))
    for i in buy_lst:
        print(tplt.format(i, buy_lst[i]['name'], buy_lst[i]['price']))
    print("即将返回主菜单".center(20, '-'))
    print()
    time.sleep(1)


def deposit_money(name):
    tplt = "{0:^5}\t{1:^5}\t{2:^5}"
    print("充值系统".center(20,'-'))
    print("1.充值10元".center(20))
    print("2.充值15元".center(20))
    select = input("对应键继续,‘exit’退出")
    if select == 'exit':
        tplt = "{0:^5}\t{1:^5}"
        print(tplt.format("账号", "余额"))
        print(tplt.format(name, user_info[name][1]))
        exit()
    elif select == '1':
        print("充值10元".center(20))
        user_info[name][1] = user_info[name][1] + 10.0
        print(tplt.format("用户", "余额", "密码"))
        print(tplt.format(name, user_info[name][1], user_info[name][0]))
    elif select == '2':
        print("充值15元".center(20))
        user_info[name][1] = user_info[name][1] + 15.0
        print(tplt.format("用户", "余额", "密码"))
        print(tplt.format(name, user_info[name][1], user_info[name][0]))
    else:
        print("输入错误!即将返回登入界面")
        menu()

name = user_login()
while name == 0:
    name = user_login()
while 1:
    a = menu()
    if a == 1:
        flag = shopping(name)
        if flag == 0:
            continue
        else:
            continue
    elif a == 2:
        buy_list(name)

